package com.live.dengdengtv.utils;

import android.app.Activity;
import android.app.Dialog;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.live.dengdengtv.R;

/**
 * Created by wyf on 2016/9/14.
 */
public class AppDialog {

    public static void showAlertDialog(Activity activity, String content , String button){

        final Dialog dialog = getDialogInstance(activity, null, Gravity.CENTER);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_alert);

        Button confirm_btn = (Button) dialog.findViewById(R.id.confirm_btn);
        confirm_btn.setText(button);
        confirm_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView content_tv = (TextView) dialog.findViewById(R.id.context_tv);
        content_tv.setText(content);
        dialog.setCancelable(true);
        dialog.show();
    }
    public static void showAlertDialog(Activity activity, String content){

        final Dialog dialog = getDialogInstance(activity, null, Gravity.CENTER);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_alert);
        dialog.findViewById(R.id.confirm_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView content_tv = (TextView) dialog.findViewById(R.id.context_tv);
        content_tv.setText(content);
        dialog.setCancelable(true);
        dialog.show();
    }


    public static Dialog getDialogInstance(Activity activity, WindowManager.LayoutParams lp, int gravity){
        Dialog dialog = new Dialog(activity);

        Window dialogWindow = dialog.getWindow();
        if(lp != null){
            dialogWindow.setAttributes(lp);
        }
        dialogWindow.setGravity(gravity);
        dialog.setCancelable(false);
        return dialog;
    }


    private static WindowManager.LayoutParams getWindowLp(Activity activity){
        DisplayMetrics outMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        return lp;
    }
}
